﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Peter.Common;
using Peter.Common.MainMenu;
using Peter.Sdw.Data;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// View model for terms.
   /// </summary>
   public sealed class TermModel : PeterDocument
   {
      private string m_Name;
      private bool m_IsLoading;
      private TermItemModel m_SelectedItem;
      private const string NEW_TERM = "New Term";
      private readonly ObservableCollection <TermItemModel> m_Items;

      /// <summary>
      /// Initializes a new book model.
      /// </summary>
      public TermModel ()
      {
         this.Title = NEW_TERM;
         this.Icon = MainMenuIcon.Tag;
         this.m_Items = new ObservableCollection<TermItemModel> ();
         this.IsSelected = true;
         this.IsActive = true;
      }

      /// <summary>
      /// Gets or Sets if it is ok to send any updates to the server.
      /// </summary>
      public bool OkToUpdate { get; set; }

      /// <summary>
      /// Gets or Sets the id of this item.
      /// </summary>
      public int Id { get; set; }

      /// <summary>
      /// Get or Sets name of this item.
      /// </summary>
      public string Name
      {
         get { return this.m_Name; }
         set
         {
            if (this.m_Name != value) {
               this.m_Name = value;
               this.RaisePropertyChanged ("Name");
            }
         }
      }

      /// <summary>
      /// Gets the list of items.
      /// </summary>
      public ObservableCollection<TermItemModel> Items { get { return this.m_Items; } }

      /// <summary>
      /// Gets or Sets the selected term item.
      /// </summary>
      public TermItemModel SelectedItem
      {
         get { return this.m_SelectedItem; }
         set
         {
            this.m_SelectedItem = value;
            this.RaisePropertyChanged ("SelectedItem");
         }
      }

      /// <summary>
      /// Gets or Sets if we are loading data.
      /// </summary>
      public bool IsLoading
      {
         get { return this.m_IsLoading; }
         set
         {
            this.m_IsLoading = value;
            this.RaisePropertyChanged ("IsLoading");
         }
      }

      /// <summary>
      /// Gets the term's items from the server.
      /// </summary>
      public void GetItems ()
      {
         this.IsLoading = true;
         this.Items.Clear ();
         WebQueue.Instance.WebGetComplete += this.OnGetCompleted;
         WebQueue.Instance.Get (string.Format (Constants.URL_TERM_ITEM_LIST, this.Id));
      }

      /// <summary>
      /// Occurs when a property of the term has changed.
      /// </summary>
      private void Edit ()
      {
         
      }

      /// <summary>
      /// Occurs when a web request is completed.
      /// </summary>
      /// <param name="sender">WebQueue.</param>
      /// <param name="e">WebRequestEventArgs</param>
      private void OnGetCompleted (object sender, WebRequestEventArgs e)
      {
         try {
            if (e.Url == string.Format (Constants.URL_TERM_ITEM_LIST, this.Id)) {
               WebQueue.Instance.WebGetComplete -= this.OnGetCompleted;
               if (e.Response.status == Constants.SUCCESS) {
                  foreach (var item in e.Response.items) {
                     /*this.Items.Add (new TermItemModel (this,
                        new TermItem {
                           Id = version.id,
                           Title = version.title,
                           DefaultReadChapter = version.defaultread,
                           Contents = version.contents,
                           PublishDate = version.publishdate,
                           SourceName = version.sourcename,
                           SourceUrl = version.sourceurl,
                           TermId = version.termid,
                           TermName = version.termname
                        }));*/
                  }
                  this.SelectedItem = this.Items.FirstOrDefault ();
               }
               this.IsLoading = false;
            }
         } catch (Exception ex) {
            Console.Error.WriteLine (ex.Message);
         }
      }
   }
}
